package com.efast.dmom.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.dmom.mes.bean.EquipSpareOutbound;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.EquipSpareOutboundDao;
import com.efast.dmom.mes.dao.entity.WmsSpareOrderLogDao;
import com.efast.dmom.mes.entity.EquipCutterPolishedRecordEntity;
import com.efast.dmom.mes.entity.EquipSpareEntity;
import com.efast.dmom.mes.entity.EquipSpareOutboundEntity;
import com.efast.dmom.mes.entity.EquipSpareStorageEntity;
import com.efast.dmom.mes.entity.WmsSpareOrderLogEntity;
import com.efast.dmom.mes.service.EquipCutterPolishedRecordService;
import com.efast.dmom.mes.service.EquipSpareOutboundService;
import com.efast.dmom.mes.service.EquipSpareService;
import com.efast.dmom.mes.service.WmsEquipSpareStorageService;
import com.efast.dmom.mes.service.WmsSpareOutInboundLogService;
import com.efast.dmom.mes.util.ServiceBaseEntityUtil;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;
import com.efast.dmom.mes.util.WmsOrderLogStatus;
import com.efast.dmom.mes.util.WmsOrderStatusUtil;

@Service
@Transactional
public class EquipSpareOutboundServiceImpl implements EquipSpareOutboundService {

	@Autowired
	private EquipSpareOutboundDao equipSpareOutboundDao;

	@Autowired
	private ServiceBaseEntityUtil serviceBaseEntityUtil;

	@Autowired
	private WmsEquipSpareStorageService wmsEquipSpareStorageService;

	@Autowired
	private WmsSpareOrderLogDao wmsSpareOrderLogDao;

	@Autowired
	private WmsSpareOutInboundLogService wmsSpareOutInboundLogService;

	@Autowired
	private EquipCutterPolishedRecordService equipCutterPolishedRecordService;

	@Autowired
	private EquipSpareService equipSpareService;

	@Override
	public ResultBean save(EquipSpareOutbound equipSpareOutbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareOutboundEntity> spareList = equipSpareOutbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			String orderNo = " ";
			boolean flag = true;
			String message = " ";
			int i = 1;
			String outboundStyle = spareList.get(0).getOutboundStyle();
			for (EquipSpareOutboundEntity entity : spareList) {
				if (entity.getSpareCategory().equals("备品备件")) {
					List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao
							.selectSpareOutbound(entity.getSpareId(), entity.getWarehouseId());
					if (entityList != null && entityList.size() > 0) {
						int storageQty = wmsEquipSpareStorageService
								.getStorageQtyBySpareIdAndWarehouseId(entity.getSpareId(), entity.getWarehouseId());
						int qty = entity.getOutboundQuantity();
						for (EquipSpareOutboundEntity data : entityList) {
							qty = qty + data.getOutboundQuantity();
						}
						if (qty > storageQty) {
							flag = false;
							if (message.equals(" ")) {
								message = i + ":备件编码为“" + entity.getSpareCode() + "”、仓库名为“" + entity.getWarehouseName()
										+ "”的备件与出库队列中的数量之和大于库存数量！";
							} else {
								i = i + 1;
								message = message.concat(i + ":备件编码为“" + entity.getSpareCode() + "”、仓库名为“"
										+ entity.getWarehouseName() + "”的备件与出库队列中的数量之和大于库存数量！");
							}
						}
					}
				} else if (entity.getSpareCategory().equals("刀具")) {
					if (entity.getCutterType().equals("刀具-有编号")) {
						List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao
								.selectSpareOutboundByCutter(entity.getSpareId());
						if (entityList != null && entityList.size() > 0) {
							flag = false;
							if (message.equals(" ")) {
								message = i + ":刀具编号为“" + entity.getCutterCode() + "”的刀具已存在于出库队列中！";
							} else {
								i = i + 1;
								message = message.concat(i + ":刀具编号为“" + entity.getCutterCode() + "”的刀具已存在于出库队列中！");
							}
						}
					} else if (entity.getCutterType().equals("刀具-无编号")) {
						List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao
								.selectSpareOutbound(entity.getSpareId(), entity.getWarehouseId());
						if (entityList != null && entityList.size() > 0) {
							int storageQty = wmsEquipSpareStorageService
									.getStorageQtyBySpareIdAndWarehouseId(entity.getSpareId(), entity.getWarehouseId());
							int qty = entity.getOutboundQuantity();
							for (EquipSpareOutboundEntity data : entityList) {
								qty = qty + data.getOutboundQuantity();
							}
							if (qty > storageQty) {
								flag = false;
								if (message.equals(" ")) {
									message = i + ":备件编码为“" + entity.getSpareCode() + "”、仓库名为“"
											+ entity.getWarehouseName() + "”无刀具编号的刀具与出库队列中的数量之和大于库存数量！";
								} else {
									i = i + 1;
									message = message.concat(i + ":备件编码为“" + entity.getSpareCode() + "”、仓库名为“"
											+ entity.getWarehouseName() + "”无刀具编号的刀具与出库队列中的数量之和大于库存数量！");
								}
							}
						}
					}
				}
			}
			if (!flag) {
				resultBean.setSuccess(false);
				resultBean.setMessage(message);
				return resultBean;
			}
			if (outboundStyle.equals("备品备件")) {
				orderNo = serviceBaseEntityUtil.getNo(WmsOrderStatusUtil.SPAREOUTBOUNDCODE);
			} else {
				orderNo = serviceBaseEntityUtil.getNo(WmsOrderStatusUtil.CUTTEROUTBOUNDCODE);
			}
			for (EquipSpareOutboundEntity entity : spareList) {
				entity.setOutboundNumber(orderNo);
				entity.setOutboundStatus(WmsOrderStatusUtil.NOCONFIRM);
				entity.setCreationTime(TimeUtil.getLocalDateTime());
				entity.setCreator(SpringWebUtils.getRequestUser());
				entity.setCompanyCode(SpringWebUtils.getRequestCompany());
				entity.setRowStatus(1);
			}
			equipSpareOutboundDao.saveBatch(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.ADD.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("保存成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存的数据为空!");
		}
		return resultBean;
	}

	@Override
	public ResultBean update(EquipSpareOutbound equipSpareOutbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareOutboundEntity> spareList = equipSpareOutbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			for (EquipSpareOutboundEntity entity : spareList) {
				if (entity.getSpareCategory().equals("备品备件")) {
					List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao
							.selectSpareOutbound(entity.getSpareId(), entity.getWarehouseId());
					int storageQty = wmsEquipSpareStorageService
							.getStorageQtyBySpareIdAndWarehouseId(entity.getSpareId(), entity.getWarehouseId());
					int qty = entity.getOutboundQuantity();
					if (entityList != null && entityList.size() > 0) {
						for (EquipSpareOutboundEntity data : entityList) {
							if (data.getId() != entity.getId()) {
								qty = qty + data.getOutboundQuantity();
							}
						}
						if (qty > storageQty) {
							resultBean.setSuccess(false);
							resultBean.setMessage("修改后的出库数量与出库队列中其它数据的数量之和大于库存数量！");
							return resultBean;
						}
					} else {
						if (qty > storageQty) {
							resultBean.setSuccess(false);
							resultBean.setMessage("修改后的出库数量大于库存数量！");
							return resultBean;
						}
					}
				}
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
				equipSpareOutboundDao.update(entity);
			}
			addSpareOperationLog(spareList, WmsOrderLogStatus.UPDATE.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("更新成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的数据为空!");
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(EquipSpareOutbound equipSpareOutbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareOutboundEntity> list = equipSpareOutbound.getSpareList();
		if (list != null && list.size() > 0) {
			List<EquipSpareOutboundEntity> spareList = equipSpareOutboundDao.getSpareOutboundList(list);
			for (EquipSpareOutboundEntity entity : spareList) {
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
			}
			equipSpareOutboundDao.deleteBatch(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.DELETE.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("删除成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("删除的数据为空!");
		}
		return resultBean;
	}

	@Override
	public ResultBean affirmSpareOutBound(List<EquipSpareOutboundEntity> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list != null && list.size() > 0) {
			List<EquipSpareOutboundEntity> spareList = equipSpareOutboundDao.getSpareOutboundList(list);
			List<EquipCutterPolishedRecordEntity> recordList = new ArrayList<EquipCutterPolishedRecordEntity>();
			for (EquipSpareOutboundEntity entity : spareList) {
				if (entity.getSpareCategory().equals("刀具") && entity.getOutboundReason().equals("刀具打磨")
						&& entity.getCutterType().equals("刀具-有编号")) {
					EquipCutterPolishedRecordEntity recordEntity = new EquipCutterPolishedRecordEntity();
					recordEntity.setSpareId(entity.getSpareId());
					recordEntity.setCutterPolishedTime(TimeUtil.getLocalDateTime());
					recordEntity.setCutterPolishedUsedTimes(0);
					recordEntity.setIsFlag(0);
					recordEntity.setCreationTime(TimeUtil.getLocalDateTime());
					recordEntity.setCreator(SpringWebUtils.getRequestUser());
					recordEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
					recordEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
					recordEntity.setLastModifier(SpringWebUtils.getRequestUser());
					recordEntity.setRowStatus(0);
					recordList.add(recordEntity);
				}
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
				entity.setOutboundStatus(WmsOrderStatusUtil.CONFIRM);
				entity.setOutboundTime(TimeUtil.getLocalDateTime());
				entity.setOutboundUser(SpringWebUtils.getRequestUser());
			}
			equipSpareOutboundDao.affirmSpareInBound(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.CONFIRM.getOperation());
			wmsSpareOutInboundLogService.addSpareOutboundLog(spareList, WmsOrderStatusUtil.OUTWAREHOUSE);

			List<EquipSpareStorageEntity> outboundList = new ArrayList<EquipSpareStorageEntity>();
			List<EquipSpareEntity> equipSpareList = new ArrayList<EquipSpareEntity>();
			for (EquipSpareOutboundEntity data : spareList) {
				EquipSpareStorageEntity stotage = new EquipSpareStorageEntity();
				EquipSpareEntity spare = new EquipSpareEntity();
				int outboundQuantity = 0;
				int outboundQty = 0;
				if (data.getSpareCategory().equals("刀具") && data.getCutterType().equals("刀具-有编号")) {
					if (data.getOutboundReason().equals("刀具打磨")) {
						stotage.setUsedStatus(WmsOrderStatusUtil.SPARE_NOENABLE_STATUS);
						spare.setUsedStatus(WmsOrderStatusUtil.SPARE_NOENABLE_STATUS);
						outboundQuantity = 0;
						outboundQty = 0;
					} else if (data.getOutboundReason().equals("生产领用")) {
						stotage.setUsedStatus(WmsOrderStatusUtil.SPARE_RECEIVE_STATUS);
						spare.setUsedStatus(WmsOrderStatusUtil.SPARE_RECEIVE_STATUS);
						outboundQuantity = 0;
						outboundQty = 0;
					} else {
						stotage.setUsedStatus(WmsOrderStatusUtil.SPARE_ENABLE_STATUS);
						spare.setUsedStatus(WmsOrderStatusUtil.SPARE_ENABLE_STATUS);
						for (EquipSpareOutboundEntity entity : spareList) {
							if (data.getSpareId() == entity.getSpareId()) {
								outboundQuantity = outboundQuantity + entity.getOutboundQuantity();
							}
							if (data.getSpareId() == entity.getSpareId()
									&& data.getWarehouseId() == entity.getWarehouseId()) {
								outboundQty = outboundQty + entity.getOutboundQuantity();
							}
						}
					}
				} else {
					stotage.setUsedStatus(WmsOrderStatusUtil.SPARE_ENABLE_STATUS);
					spare.setUsedStatus(WmsOrderStatusUtil.SPARE_ENABLE_STATUS);
					for (EquipSpareOutboundEntity entity : spareList) {
						if (data.getSpareId() == entity.getSpareId()) {
							outboundQuantity = outboundQuantity + entity.getOutboundQuantity();
						}
						if (data.getSpareId() == entity.getSpareId()
								&& data.getWarehouseId() == entity.getWarehouseId()) {
							outboundQty = outboundQty + entity.getOutboundQuantity();
						}
					}
				}
				stotage.setWarehouseId(data.getWarehouseId());
				stotage.setSpareId(data.getSpareId());
				spare.setId(data.getSpareId());
				spare.setLastModifiedTime(TimeUtil.getLocalDateTime());
				spare.setLastModifier(SpringWebUtils.getRequestUser());
				stotage.setStorageQuality(outboundQty);
				stotage.setTotalAmount(outboundQty * data.getUnitPrice());
				spare.setInventory(outboundQuantity);
				outboundList.add(stotage);
				equipSpareList.add(spare);
			}
			for (int i = 0; i < outboundList.size() - 1; i++) {
				for (int j = outboundList.size() - 1; j > i; j--) {
					if (outboundList.get(j).getSpareId() == outboundList.get(i).getSpareId()
							&& outboundList.get(j).getWarehouseId() == outboundList.get(i).getWarehouseId()) {
						outboundList.remove(j);
					}
				}
			}
			for (int i = 0; i < equipSpareList.size() - 1; i++) {
				for (int j = equipSpareList.size() - 1; j > i; j--) {
					if (equipSpareList.get(j).getId() == equipSpareList.get(i).getId()) {
						equipSpareList.remove(j);
					}
				}
			}
			wmsEquipSpareStorageService.decreaseSpareStorage(outboundList);
			equipSpareService.decreaseInventoryBySpareId(equipSpareList);
			if (recordList != null && recordList.size() > 0) {
				equipCutterPolishedRecordService.addCutterGrindRecord(recordList);
			}
			resultBean.setSuccess(true);
			resultBean.setMessage("确认成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("确认的数据为空!");
		}
		return resultBean;
	}

	public void addSpareOperationLog(List<EquipSpareOutboundEntity> spareList, String operation) {
		if (spareList != null && spareList.size() > 0) {
			List<WmsSpareOrderLogEntity> logList = new ArrayList<WmsSpareOrderLogEntity>();
			for (EquipSpareOutboundEntity entity : spareList) {
				WmsSpareOrderLogEntity logEntity = new WmsSpareOrderLogEntity();
				logEntity.setOutInboundOrderId(entity.getId());
				logEntity.setOutInboundOrderNo(entity.getOutboundNumber());
				logEntity.setOrderOperationStatus(operation);
				logEntity.setCreationTime(TimeUtil.getLocalDateTime());
				logEntity.setCreator(SpringWebUtils.getRequestUser());
				logEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
				logList.add(logEntity);
			}
			wmsSpareOrderLogDao.saveBatch(logList);
		}
	}

	@Override
	public ResultBean saveItem(EquipSpareOutbound equipSpareOutbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareOutboundEntity> spareList = equipSpareOutbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			for (EquipSpareOutboundEntity entity : spareList) {
				if (entity.getSpareCategory().equals("备品备件")) {
					List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao
							.selectSpareOutbound(entity.getSpareId(), entity.getWarehouseId());
					int storageQty = wmsEquipSpareStorageService
							.getStorageQtyBySpareIdAndWarehouseId(entity.getSpareId(), entity.getWarehouseId());
					int qty = entity.getOutboundQuantity();
					if (entityList != null && entityList.size() > 0) {
						for (EquipSpareOutboundEntity data : entityList) {
							qty = qty + data.getOutboundQuantity();
						}
						if (qty > storageQty) {
							resultBean.setSuccess(false);
							resultBean.setMessage("备件编码为“" + entity.getSpareCode() + "”、仓库名为“"
									+ entity.getWarehouseName() + "”的备件与出库队列中的数量之和大于库存数量！");
							return resultBean;
						}
					} else {
						if (qty > storageQty) {
							resultBean.setSuccess(false);
							resultBean.setMessage("备件编码为“" + entity.getSpareCode() + "”、仓库名为“"
									+ entity.getWarehouseName() + "”的备件的出库数量大于库存数量！");
							return resultBean;
						}
					}
					List<EquipSpareOutboundEntity> entityDataList = equipSpareOutboundDao
							.selectSpareOutBoundList(entity);
					if (entityDataList != null && entityDataList.size() > 0) {
						resultBean.setSuccess(false);
						resultBean.setMessage("该备件入库单号下已存在出“" + entity.getWarehouseName() + "”仓库的备件！");
						return resultBean;
					}
				} else if (entity.getSpareCategory().equals("刀具")) {
					if (entity.getCutterType().equals("刀具-有编号")) {
						List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao
								.selectSpareOutboundByCutter(entity.getSpareId());
						if (entityList != null && entityList.size() > 0) {
							resultBean.setSuccess(false);
							resultBean.setMessage("刀具编号为“" + entity.getCutterCode() + "”的刀具已存在于出库队列中！");
							return resultBean;
						}
					} else {
						List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao
								.selectSpareOutbound(entity.getSpareId(), entity.getWarehouseId());
						int storageQty = wmsEquipSpareStorageService
								.getStorageQtyBySpareIdAndWarehouseId(entity.getSpareId(), entity.getWarehouseId());
						int qty = entity.getOutboundQuantity();
						if (entityList != null && entityList.size() > 0) {
							for (EquipSpareOutboundEntity data : entityList) {
								qty = qty + data.getOutboundQuantity();
							}
							if (qty > storageQty) {
								resultBean.setSuccess(false);
								resultBean.setMessage("备件编码为“" + entity.getSpareCode() + "”、仓库名为“"
										+ entity.getWarehouseName() + "”无刀具编号的刀具与出库队列中的数量之和大于库存数量！");
								return resultBean;
							}
						} else {
							if (qty > storageQty) {
								resultBean.setSuccess(false);
								resultBean.setMessage("备件编码为“" + entity.getSpareCode() + "”、仓库名为“"
										+ entity.getWarehouseName() + "”无刀具编号的刀具的出库数量大于库存数量！");
								return resultBean;
							}
						}
						List<EquipSpareOutboundEntity> entityDataList = equipSpareOutboundDao
								.selectSpareOutBoundList(entity);
						if (entityDataList != null && entityDataList.size() > 0) {
							resultBean.setSuccess(false);
							resultBean.setMessage("该备件入库单号下已存在出“" + entity.getWarehouseName() + "”仓库无刀具编号的刀具！");
							return resultBean;
						}
					}
				}
				entity.setOutboundNumber(entity.getOutboundNumber());
				entity.setOutboundStatus(WmsOrderStatusUtil.NOCONFIRM);
				entity.setCreationTime(TimeUtil.getLocalDateTime());
				entity.setCreator(SpringWebUtils.getRequestUser());
				entity.setCompanyCode(SpringWebUtils.getRequestCompany());
				entity.setRowStatus(1);
			}
			equipSpareOutboundDao.saveBatch(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.ADD.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("保存成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存的数据为空!");
		}
		return resultBean;
	}

	public ResultBean checkSparePart(EquipSpareOutboundEntity entity) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao.selectSpareOutbound(entity.getSpareId(),
				entity.getWarehouseId());
		int storageQty = wmsEquipSpareStorageService.getStorageQtyBySpareIdAndWarehouseId(entity.getSpareId(),
				entity.getWarehouseId());
		int qty = entity.getOutboundQuantity();
		if (entityList != null && entityList.size() > 0) {
			for (EquipSpareOutboundEntity data : entityList) {
				qty = qty + data.getOutboundQuantity();
			}
			if (qty > storageQty) {
				resultBean.setSuccess(false);
				resultBean.setMessage("备件编码为“" + entity.getSpareCode() + "”、仓库名为“" + entity.getWarehouseName()
						+ "”的备件与出库队列中的数量之和大于库存数量！");
				return resultBean;
			}
		} else {
			if (qty > storageQty) {
				resultBean.setSuccess(false);
				resultBean.setMessage(
						"备件编码为“" + entity.getSpareCode() + "”、仓库名为“" + entity.getWarehouseName() + "”的备件的出库数量大于库存数量！");
				return resultBean;
			}
		}
		List<EquipSpareOutboundEntity> entityDataList = equipSpareOutboundDao.selectSpareOutBoundList(entity);
		if (entityDataList != null && entityDataList.size() > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该备件入库单号下已存在出“" + entity.getWarehouseName() + "”仓库的备件！");
			return resultBean;
		}
		return resultBean;
	}

	public ResultBean checkNoCodeCutter(EquipSpareOutboundEntity entity) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao.selectSpareOutbound(entity.getSpareId(),
				entity.getWarehouseId());
		int storageQty = wmsEquipSpareStorageService.getStorageQtyBySpareIdAndWarehouseId(entity.getSpareId(),
				entity.getWarehouseId());
		int qty = entity.getOutboundQuantity();
		if (entityList != null && entityList.size() > 0) {
			for (EquipSpareOutboundEntity data : entityList) {
				qty = qty + data.getOutboundQuantity();
			}
			if (qty > storageQty) {
				resultBean.setSuccess(false);
				resultBean.setMessage("备件编码为“" + entity.getSpareCode() + "”、仓库名为“" + entity.getWarehouseName()
						+ "”无刀具编号的刀具与出库队列中的数量之和大于库存数量！");
				return resultBean;
			}
		} else {
			if (qty > storageQty) {
				resultBean.setSuccess(false);
				resultBean.setMessage("备件编码为“" + entity.getSpareCode() + "”、仓库名为“" + entity.getWarehouseName()
						+ "”无刀具编号的刀具的出库数量大于库存数量！");
				return resultBean;
			}
		}
		List<EquipSpareOutboundEntity> entityDataList = equipSpareOutboundDao.selectSpareOutBoundList(entity);
		if (entityDataList != null && entityDataList.size() > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该备件入库单号下已存在出“" + entity.getWarehouseName() + "”仓库无刀具编号的刀具！");
			return resultBean;
		}
		return resultBean;
	}

	public ResultBean checkOutBoundQuantity(EquipSpareOutboundEntity entity) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao.selectSpareOutbound(entity.getSpareId(),
				entity.getWarehouseId());
		int storageQty = wmsEquipSpareStorageService.getStorageQtyBySpareIdAndWarehouseId(entity.getSpareId(),
				entity.getWarehouseId());
		int qty = entity.getOutboundQuantity();
		boolean flag = true;
		if (entityList != null && entityList.size() > 0) {
			for (EquipSpareOutboundEntity data : entityList) {
				if (data.getId() != entity.getId()) {
					flag = false;
					qty = qty + data.getOutboundQuantity();
				}
			}
			if (qty > storageQty && !flag) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改后的出库数量与出库队列中其它数据的数量之和大于库存数量！");
				return resultBean;
			} else if (qty > storageQty) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改后的出库数量大于库存数量！");
				return resultBean;
			}
		}
		return resultBean;
	}

	@Override
	public ResultBean updateItem(EquipSpareOutbound equipSpareOutbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareOutboundEntity> spareList = equipSpareOutbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			for (EquipSpareOutboundEntity entity : spareList) {
				EquipSpareOutboundEntity beforeUpdateData = equipSpareOutboundDao.getById(entity.getId());
				if (beforeUpdateData.getSpareId() != entity.getSpareId()) {
					if (entity.getSpareCategory().equals("备品备件")) {
						ResultBean result = checkSparePart(entity);
						if (!result.isSuccess()) {
							resultBean.setSuccess(false);
							resultBean.setMessage(result.getMessage());
							return resultBean;
						}
					} else if (entity.getSpareCategory().equals("刀具")) {
						if (entity.getCutterType().equals("刀具-有编号")) {
							List<EquipSpareOutboundEntity> entityList = equipSpareOutboundDao
									.selectSpareOutboundByCutter(entity.getSpareId());
							if (entityList != null && entityList.size() > 0) {
								resultBean.setSuccess(false);
								resultBean.setMessage("刀具编号为“" + entity.getCutterCode() + "”的刀具已存在于出库队列中！");
								return resultBean;
							}
						} else {
							ResultBean result = checkNoCodeCutter(entity);
							if (!result.isSuccess()) {
								resultBean.setSuccess(false);
								resultBean.setMessage(result.getMessage());
								return resultBean;
							}
						}
					}
				}
				if (entity.getSpareId() == beforeUpdateData.getSpareId() && entity.getSpareCategory().equals("刀具")
						&& entity.getCutterType().equals("刀具-无编号")
						&& entity.getWarehouseId() != beforeUpdateData.getWarehouseId()) {
					ResultBean result = checkNoCodeCutter(entity);
					if (!result.isSuccess()) {
						resultBean.setSuccess(false);
						resultBean.setMessage(result.getMessage());
						return resultBean;
					}
				}
				if (entity.getSpareId() == beforeUpdateData.getSpareId() && entity.getSpareCategory().equals("备品备件")
						&& entity.getWarehouseId() != beforeUpdateData.getWarehouseId()) {
					ResultBean result = checkSparePart(entity);
					if (!result.isSuccess()) {
						resultBean.setSuccess(false);
						resultBean.setMessage(result.getMessage());
						return resultBean;
					}
				}
				if (entity.getSpareId() == beforeUpdateData.getSpareId()
						&& entity.getWarehouseId() != beforeUpdateData.getWarehouseId()
						&& entity.getSpareCategory().equals("备品备件")
						&& entity.getOutboundQuantity() != beforeUpdateData.getOutboundQuantity()) {
					ResultBean result = checkOutBoundQuantity(entity);
					if (!result.isSuccess()) {
						resultBean.setSuccess(false);
						resultBean.setMessage(result.getMessage());
						return resultBean;
					}
				}
				if (entity.getSpareId() == beforeUpdateData.getSpareId()
						&& entity.getWarehouseId() != beforeUpdateData.getWarehouseId()
						&& entity.getSpareCategory().equals("刀具")
						&& entity.getOutboundQuantity() != beforeUpdateData.getOutboundQuantity()
						&& entity.getCutterType().equals("刀具-无编号")) {
					ResultBean result = checkOutBoundQuantity(entity);
					if (!result.isSuccess()) {
						resultBean.setSuccess(false);
						resultBean.setMessage(result.getMessage());
						return resultBean;
					}
				}
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
				equipSpareOutboundDao.update(entity);
			}
			addSpareOperationLog(spareList, WmsOrderLogStatus.UPDATE.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("更新成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的数据为空!");
		}
		return resultBean;
	}

	@Override
	public ResultBean deleteItem(EquipSpareOutbound equipSpareOutbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareOutboundEntity> spareList = equipSpareOutbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			for (EquipSpareOutboundEntity entity : spareList) {
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
			}
			equipSpareOutboundDao.deleteBatch(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.DELETE.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("删除成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("删除的数据为空!");
		}
		return resultBean;
	}

	@Override
	public List<EquipSpareOutboundEntity> getOutBoundEntityList(long spareId, String outboundReason) {
		List<EquipSpareOutboundEntity> outBoundEntityList = equipSpareOutboundDao.getOutBoundEntityList(spareId,
				outboundReason);
		return outBoundEntityList;
	}

}
